package eu.flatworld.android.ontop.db;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;


public class Util {
	
	
	public static Memo createMemo(DatabaseHelper dh, String name) throws SQLException {

			Memo m = new Memo();
			m.setName(name);
			m.setMemoType(MemoType.CHECKLIST);
			dh.getMemoDao().create(m);
			return m;
	}

	public static void deleteMemo(DatabaseHelper dh, Memo m) throws SQLException {
			dh.getMemoDao().delete(m);
	}

	public static List<Memo> loadMemoList(DatabaseHelper dh) throws SQLException {
			Dao<Memo, Integer> md = dh.getMemoDao();
			List<Memo> lm = md.queryForAll();
			return lm;
	}
	
	public static ChecklistItem createCheclistItem(DatabaseHelper dh, Memo m, String name) throws SQLException {
			ChecklistItem ci = new ChecklistItem();
			ci.setName(name);
			ci.setMemo(m);
			dh.getChecklistItemDao().create(ci);
			return ci;
	}

	public static void deleteChecklistItem(DatabaseHelper dh, ChecklistItem ci) throws SQLException {
			dh.getChecklistItemDao().delete(ci);
	}
	
	public static void deleteChecklistItems(DatabaseHelper dh, Memo m) throws SQLException {
		List<ChecklistItem> l = loadChecklistItemList(dh, m);
		for(ChecklistItem ci : l) {
			Util.deleteChecklistItem(dh, ci);
		}
	}

	public static List<ChecklistItem> loadChecklistItemList(DatabaseHelper dh,
			Memo m) throws SQLException {
			Dao<ChecklistItem, Integer> cid = dh.getChecklistItemDao();			
			List<ChecklistItem> lm = cid.queryBuilder().where().eq("memo_id", m.getId()).query();
			return lm;
	}
	
	public static ChecklistItem update(DatabaseHelper dh, ChecklistItem ci) throws SQLException {
		dh.getChecklistItemDao().update(ci);
		return ci;
	}
}
